package com.central.apps.handler.purchase;

import com.central.app.service.HandlerService;
import com.central.apps.common.constant.InventoryConstant;
import com.central.apps.handler.base.LvBaseHandler;
import com.central.common.exception.service.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Slf4j
@Component
public class MaterialMasterDataHandler extends LvBaseHandler {
    @Autowired
    private HandlerService handlerService;

    @Override
    public void beforSave(String app, String objName, Map<String, Object> data, boolean isUpdate) throws Exception {
//        if ("itemApp".equals(app)){
//            if (isUpdate==false){
//                if ("gq_item".equals(objName)){
//                    String description = MapUtils.getString(data, "description");
//                    String model = MapUtils.getString(data, "model");
//                    Integer id = MapUtils.getInteger(data, "id");
//                    List<Map<String, Object>> bySql1 = handlerService.findBySql("select * from gq_item where description='"+description+"' and model='"+model+"'");
//                    if (bySql1 !=null && bySql1.size()>0){
//                        Map<String, Object> map = bySql1.get(0);
//                        throw new ServiceException("物料名称与型号已存在，请重新输入");
//                    }
//
//
//                }
//            }else {
//                if ("gq_item".equals(objName)){
//                    String description = MapUtils.getString(data, "description");
//                    String model = MapUtils.getString(data, "model");
//                    Integer id = MapUtils.getInteger(data, "id");
//                    String status = MapUtils.getString(data, "status");
//                    List<Map<String, Object>> bySql1 = handlerService.findBySql("select * from gq_item where description='"+description+"' and model='"+model+"'");
//                    if (bySql1 !=null && bySql1.size()>0){
//                        Map<String, Object> map = bySql1.get(0);
//                        throw new ServiceException("物料名称与型号已存在，请重新输入");
//                    }
//
//                }
//            }
//        }
        if ("itemApp".equals(app)) {
            //1.新增数据保存2.修改数据保存
                if ("gq_item".equals(objName)) {
                    String description = (String) data.get("description");
                    String model = (String) data.get("model");
                    String specifications = MapUtils.getString(data, "specifications");
                    if (StringUtils.isEmpty(specifications)){
                        String sql = "select * from gq_item where model ='" + model + "' and description = '" + description + "'";
                        List<Map<String, Object>> res = handlerService.findBySql(sql);
                        if(!isUpdate){//新增保存
                            if(data.get("description")!=null&&data.get("model")!=null){
                                if (CollectionUtils.isNotEmpty(res)) {
                                    throw new ServiceException("物料名称为" + description + ",型号为" + model + "的物料主数据已存在，请修改后再保存。");
                                }
                            }
                        }else{//修改时保存
                            Integer id = MapUtils.getInteger(data, "id");
                            if(StringUtils.isNotBlank(description)&&StringUtils.isNotBlank(model)){
                                if(CollectionUtils.isNotEmpty(res)){
                                    for (Map<String, Object> re : res) {
                                        Integer id1 = MapUtils.getInteger(re, "id");
                                        if(!id.equals(id1)){
                                            throw new ServiceException("物料名称为" + description + ",型号为" + model + "的物料主数据已存在，请修改后再保存。");
                                        }
                                    }
                                }
                            }
                        }
                    }else {
                        String sql = "select * from gq_item where model ='" + model + "' and description = '" + description + "' and specifications='"+specifications+"'";
                        List<Map<String, Object>> res = handlerService.findBySql(sql);
                        if(!isUpdate){//新增保存
                            if(data.get("description")!=null&&data.get("model")!=null){
                                if (CollectionUtils.isNotEmpty(res)) {
                                    throw new ServiceException("物料名称为" + description + ",型号为" + model + ",规格为"+specifications+"的物料主数据已存在，请修改后再保存。");
                                }
                            }
                        }else{//修改时保存
                            Integer id = MapUtils.getInteger(data, "id");
                            if(StringUtils.isNotBlank(description)&&StringUtils.isNotBlank(model)){
                                if(CollectionUtils.isNotEmpty(res)){
                                    for (Map<String, Object> re : res) {
                                        Integer id1 = MapUtils.getInteger(re, "id");
                                        if(!id.equals(id1)){
                                            throw new ServiceException("物料名称为" + description + ",型号为" + model + ",规格为"+specifications+"的物料主数据已存在，请修改后再保存。");
                                        }
                                    }
                                }
                            }
                        }
                    }

                    //物料类型串存数据
                    String maxnum = MapUtils.getString(data, "maxnum");
                    String mediumnum = MapUtils.getString(data, "mediumnum");
                    String smallnum = MapUtils.getString(data, "smallnum");
                    data.put("materialtypestring",maxnum+mediumnum+smallnum);
                }

        }
        super.beforSave(app, objName, data, isUpdate);
    }

    @Override
    public void afterSave(String app, String objName, Map<String, Object> data, boolean isUpdate) throws
            Exception {
        if ("itemApp".equals(app)) {
            if ("gq_item".equals(objName)) {
                Integer id = MapUtils.getInteger(data, "id");
                String status = MapUtils.getString(data, "status");
                if (MapUtils.getString(data, "itemnum")==null||"".equals(MapUtils.getString(data, "itemnum"))) {
                    List<Map<String, Object>> bySql = handlerService.findBySql("select * from gq_item where id =" + id);
                    if (CollectionUtils.isNotEmpty(bySql)) {
                        Map<String, Object> map = bySql.get(0);
                        String maxnum1 = MapUtils.getString(map, "maxnum");
                        String maxnum = "CD" + maxnum1;
                        String mediumnum = MapUtils.getString(map, "mediumnum");
                        String smallnum = MapUtils.getString(map, "smallnum");
                        //查物料编号

                        Integer itemnum = InventoryConstant.ITEMNUM_INIT_IDX;
                        //String itemnumSql ="select SUBSTRING(itemnum,6) as itemnum from gq_materappline where itemnum like '"+categorycode+middlecode+subcatecode+"%'order by SUBSTRING(itemnum,6) desc limit 1";
                        String itemnumSql = "select SUBSTRING(itemnum,8) as itemnum from gq_item where itemnum like '" + maxnum + mediumnum + smallnum + "%'order by SUBSTRING(itemnum,8) desc limit 1";
                        List<Map<String, Object>> itemnumList = handlerService.findBySql(itemnumSql);
                        if (itemnumList != null && itemnumList.size() > 0) {
                            Map<String, Object> map1 = itemnumList.get(0);
                            String itemnum1 = MapUtils.getString(map1, "itemnum");
                            if (StringUtils.isNotBlank(itemnum1)) {
                                String substring = itemnum1.substring(itemnum1.length() - 6);
                                itemnum = Integer.parseInt(substring);
                            }
                        }
                        itemnum = itemnum + 1;
                        StringBuilder stringBuilder = new StringBuilder();
                        stringBuilder.append(maxnum + mediumnum + smallnum + itemnum);
                        Integer materId = MapUtils.getInteger(map, "id");
                        String updateMaterapplineSql = "update gq_item set itemnum =" + "'" + stringBuilder.toString() + "'" + " where id=" + materId;
                        handlerService.updateBySql(updateMaterapplineSql);

                        //回显物料类型，使用了该大类，则大类下的中小，都不让修改
                        handlerService.findBySql("update gq_itemtype set isitemtypeuser = '2' where maxnum = '" + maxnum1 + "'");
                        handlerService.findBySql("update gq_medium set ismediumuser = '2' where maxnum = '" + maxnum1 + "' and mediumnum = '" + mediumnum + "'");
                        handlerService.findBySql("update gq_small set issmalluser = '2' where maxnum = '" + maxnum1 + "' and mediumnum = '" + mediumnum + "' and smallnum = '" + smallnum + "'");
                    }
                }
            }
        }
        super.afterSave(app, objName, data, isUpdate);
    }

    @Override
    public String validateDelete(String app, String objName, Long id) throws Exception {
        if ("gq_item".equals(objName)){
            List<Map<String, Object>> bySql = handlerService.findBySql("select * from gq_item where id=" + id);
            String itemnum = "";
            if (CollectionUtils.isNotEmpty(bySql)){
                Map<String, Object> map = bySql.get(0);
                String status = MapUtils.getString(map, "status");
                itemnum = MapUtils.getString(map, "itemnum");
                if (status.equals("已启用")){
                    return "不允许删除已启用的主数据！";
                }
            }
            //库存台账中，该物料存在库存余量，不应该删除
            //只要库存台账存在物料主数据，就不可以删除物料主数据
            List<Map<String, Object>> bySql1 = handlerService.findBySql("SELECT * from gq_inventory  where itemnum = '" + itemnum + "'");
            if(CollectionUtils.isNotEmpty(bySql1)){
                Map<String, Object> map = bySql1.get(0);
                String id1 = MapUtils.getString(map, "id");
                if (StringUtils.isNotBlank(id1)) {
                    return "不允许删除库存台账存在的物料主数据！";
                } else {

                }
            }else {

            }
        }
        return super.validateDelete(app, objName, id);
    }
}
